﻿using LD.BC;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmFobAoi : BaseFrm
    {
        private string id = Guid.NewGuid().ToString();
        public FrmFobAoi()
        {
            InitializeComponent();
            this.Shown += FrmFobAoi_Shown;
            this.FormClosing += FrmFobAoi_FormClosing;
        }

        private void FrmFobAoi_FormClosing(object sender, FormClosingEventArgs e)
        {
            PubSub.Hub.Default.Unsubscribe(id);
        }

        private void FrmFobAoi_Shown(object sender, EventArgs e)
        {
            PubSub.Hub.Default.Subscribe<StateEnum>(id, StateEnumChangeNotity);

            this.txt_max.Text = Cache.GetCache(CONST.FOBAOI_TESTMAX).ToString();
            this.txt_min.Text = Cache.GetCache(CONST.FOBAOI_TESTMIN).ToString();
            this.txt_moveMM.Text = Cache.GetCache(CONST.FOBAOI_TESTMOVE).ToString();
            this.sw_mes.Checked = (bool)Cache.GetCache(CONST.DPT_ISMES);
        }

        private void btn_point_Click(object sender, EventArgs e)
        {
            if (!(bool)Cache.GetCache(CONST.BC_ISRESET))
            {
                Log4.Warr("请先复位");
                return;
            }
            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                var btn = sender as HopeRoundButton;
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Success;

                TB_Point point = Appbus.PartNo.Config.AxisPoint
                                        .Where(x => x.Name == btn.Tag.ToString()).SingleOrDefault();
                if (point == null)
                    Log4.Warr($"找不到点位【{btn.Text}】");
                else
                    point.Move(true);
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            }
            else
                Log4.Warr("自动模式不允许手动运行");
        }

        private void btnIOClose_Click(object sender, EventArgs e)
        {
            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                HopeButton me = sender as HopeButton;
                BcFactory.Instance.SetOut(me.Tag.ToString(), false);
            }
        }
        private void btnIOOpen_Click(object sender, EventArgs e)
        {
            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                HopeButton me = sender as HopeButton;
                BcFactory.Instance.SetOut(me.Tag.ToString(), true);
            }
        }
        private void btn_reset_Click(object sender, EventArgs e)
        {
            if (BCAlertHelper.GetErrorQty) return;

            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                BcFactory.Instance.StartStop.Reset();
            }
        }
        private void StateEnumChangeNotity(StateEnum state)
        {
            if (state == StateEnum.手动模式)
            {
                if (!this.IsDisposed)
                {
                    this.Invoke(new Action(() =>
                    {

                    }));
                }
            }
        }
        public void Save()
        {
            if (double.TryParse(this.txt_min.Text, out var min))
                Cache.SetCache(CONST.FOBAOI_TESTMIN, min);
            if (double.TryParse(this.txt_min.Text, out var max))
                Cache.SetCache(CONST.FOBAOI_TESTMAX, max);
            if (double.TryParse(this.txt_moveMM.Text, out var moveMM))
                Cache.SetCache(CONST.FOBAOI_TESTMOVE, moveMM);
            Cache.SetCache(CONST.DPT_ISMES, this.sw_mes.Checked);
        }

        private void btn_testOne_Click(object sender, EventArgs e)
        {
            string res = CommunicateFactory.Instance.TcpIp["Keyence"].Send("R0\r", 1000);
            Log4.Info($"【keyence】:" + res);
        }

        private void btn_test_Click(object sender, EventArgs e)
        {

        }
    }
}
